﻿
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System;

namespace QLab.IO.Files
{
  public class ProjectFileInfo
  {
    protected string _fileName = String.Empty;
    protected string _name = String.Empty;

    protected List<string> _projectFolders = new List<string>();

    /// <summary>
    /// Input file format:
    /// 
    /// QLab_TestProject_01.qproj
    /// ---------------------------------------
    ///   ProjectFolder = "TestProject_01"
    ///   ProjectFolder = "TestProject_02"
    ///   ProjectFolder = "TestProject_03"
    ///   ...
    /// ---------------------------------------
    /// 
    /// </summary>
    /// <param name="FileName"></param>
    /// <returns></returns>
    public bool Load(string FileName)
    {
      const string pattern = @"ProjectFolder\s*=\s*\""(?<path>.+)\""";

      _projectFolders.Clear();

      _name = System.IO.Path.GetFileNameWithoutExtension(FileName);
      _fileName = FileName;

      string text = CustomFile.LoadText(_fileName);
      string root_folder = System.IO.Path.GetDirectoryName(_fileName);

      MatchCollection matches = Regex.Matches(text, pattern, RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled);
      if (matches.Count.Equals(0))
        return false;

      foreach (Match m in matches)
      {
        string value = m.Groups["path"].Value.Trim().ToLower();
        if (!String.IsNullOrEmpty(value))
          if(_projectFolders.IndexOf(value).Equals(-1))
          _projectFolders.Add(System.IO.Path.Combine(root_folder, value));
      }

      return true;
    }

    public List<string> ProjectFolders
    {
      get { return _projectFolders; }
    }

    public string Name
    {
      get { return _name; }
    }

    public string Path
    {
      get { return _fileName; }
    }
  }
}
